//
//  PingJiaViewController.m
//  随便点
//
//  Created by MWJ on 15/4/8.
//  Copyright (c) 2015年 qfsoft. All rights reserved.
//

#import "PingJiaViewController.h"
#import "PingJiaView.h"
#import "AppDelegate.h"
#define Kxianshi @"http://www.51anys.com:8080/waimaiwang/order/details"
#define Ktj @"http://www.51anys.com:8080/waimaiwang/order/evaluate"

@interface PingJiaViewController (){
    PingJiaView *pingJiaView;
    AppDelegate *app;
    NSDictionary *dic;
    NSDictionary *dic1;
    NSDictionary *xinxi;
    NSDictionary *caiming;
    int Y;
    UIAlertView *_alert;
}

@end

@implementation PingJiaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    // Do any additional setup after loading the view.
    
    //加载自定义的scrollview
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PingJiaView" owner:self options:nil];
    pingJiaView = nib[0];
    
    //框架scrollview的大小
    self.PingJiaScrollView.contentSize = CGSizeMake(self.view.frame.size.width, pingJiaView.frame.size.height);
    
    //把自定义的scrollview添加到框架scrollview上
    [self.PingJiaScrollView addSubview:pingJiaView];
    
    //起始高度
    Y = 5;
    [self Request];
    
    pingJiaView.dptp.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:app.dppic]];
    pingJiaView.dpmc.text = [NSString stringWithFormat:@"%@",app.dpname];
    [pingJiaView.tijiao addTarget:self action:@selector(tj) forControlEvents:UIControlEventTouchUpInside];
}
-(void)tj{
    int lab1 = [pingJiaView.label1.text intValue];
    int lab2 = [pingJiaView.label2.text intValue];
    int lab3 = [pingJiaView.label3.text intValue];
    NSURL *url=[NSURL URLWithString:Ktj];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSString *postStr=[NSString stringWithFormat:@"orderId=%@&content=%@&server=%d&taste=%d&speed=%d",app.DDID,pingJiaView.neirong.text,lab1,lab2,lab3];
    [request setHTTPMethod:@"post"];
    NSData *data=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
        dic1 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:dic1 waitUntilDone:YES];
    }];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提交成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    _alert = alert;
    [alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0&&alertView==_alert) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(void)loadDishes:(NSMutableArray *)dishesArray Price:(NSMutableArray *)priceArray Number:(NSMutableArray *)numberArray{
    
    for (int i=0 ; i<_MutArray.count; i++) {
        [pingJiaView SetupDishesName:dishesArray[i] Price:priceArray[i] Number:numberArray[i] SetY:Y];
        //每添加一行，Y值就 ＋20
        Y+=20;
    }
    NSLog(@"%d",Y);
    //第一个view的高度在原来的基础上 ＋10 成为最新高度
    pingJiaView.View1.frame = CGRectMake(0, pingJiaView.View1.frame.origin.y, self.view.frame.size.width, Y + 10 );
    
    //第二个view的 Y 值在原来的基础上 ＋ 第一个view的最新高度
    pingJiaView.View2.frame = CGRectMake(0, pingJiaView.View1.frame.origin.y + Y +10, self.view.frame.size.width, pingJiaView.View2.frame.size.height);
    
    //整个scrollview的高度，在原有的高度上 ＋ 第一个view的最新高度
    self.PingJiaScrollView.contentSize = CGSizeMake(self.view.frame.size.width, pingJiaView.frame.size.height + Y +10);
}
-(void)Request{
    NSURL *url=[NSURL URLWithString:Kxianshi];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSString *postStr=[NSString stringWithFormat:@"orderId=%@",app.DDID];
    [request setHTTPMethod:@"post"];
    NSData *data=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
        dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:dic waitUntilDone:YES];
        self.MutArray = [NSMutableArray array];
        xinxi = dic[@"order"];
        self.MutArray = dic[@"goodsList"];
//        NSLog(@"11111%@",dic);
        pingJiaView.ddh.text = [NSString stringWithFormat:@"%@",xinxi[@"id"]];
        pingJiaView.xm.text = [NSString stringWithFormat:@"%@",xinxi[@"username"] ];
        pingJiaView.dh.text = [NSString stringWithFormat:@"%@",xinxi[@"phone"] ];
        pingJiaView.ddsj.text = [NSString stringWithFormat:@"%@",xinxi[@"startTime"] ];
        pingJiaView.shdz.text = [NSString stringWithFormat:@"%@",xinxi[@"address"] ];
        [self caim];
//        pingJiaView.splb.text = [NSString stringWithFormat:@"%@",caiming[@"goodsName"] ];
    }];
}
-(void)refreshData:(NSArray *)sender{
}

-(void)caim{
    //菜名数组
    NSMutableArray *array=[NSMutableArray array];
    NSMutableArray *array1=[NSMutableArray array];
    NSMutableArray *array2=[NSMutableArray array];
    
    for (NSDictionary *smallarray in _MutArray) {
            if (![smallarray[@"count"]  isEqual: @"0"]) {
                [array addObject: smallarray[@"goodsName"]];
                [array1 addObject: smallarray[@"count"]];
                [array2 addObject: smallarray[@"price"]];
        }
    }
    
    // 加载第一个view上的订单信息
    [self loadDishes:array Price:array2 Number:array1];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [pingJiaView.neirong resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
